import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil


class TestUserManagement:

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/4_get_user_list.yaml'))
    def test_get_user_list(self, userinfo, get_env_info):
        # 获取用户列表接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/1_create_user.yaml'))
    def test_create_user(self, userinfo, get_env_info):
        # 创建用户接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']
        method = userinfo['request']['method']
        data = userinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        # print(req.content)
        globals()['username'] = req.json()['Data']['Username']
        # print(globals()['username'])
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/3_modify_user.yaml'))
    def test_update_user(self, userinfo, get_env_info):
        # 修改用户接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url'].replace('{username}',globals()['username'])
        method = userinfo['request']['method']
        userinfo['request']['data']['Username']=globals()['username']
        data = userinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        # print(req.content)
        globals()['username'] = req.json()['Data']['Username']
        # print(globals()['username'])
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/2_get_user_detail.yaml'))
    def test_get_user_detail(self, userinfo, get_env_info):
        # 获取用户详情接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url'].replace('{username}',globals()['username'])
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/5_get_platform_role.yaml'))
    def test_get_cluster_user_list(self, userinfo, get_env_info):
        # 获取平台级角色列表接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/6_get_cluster_role.yaml'))
    def test_get_cluster_user_list(self, userinfo, get_env_info):
        # 获取集群级角色列表接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/7_get_cluster_user_list.yaml'))
    def test_get_cluster_user_list(self, userinfo, get_env_info):
        # 获取集群成员列表接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/8_get_invite_user_list.yaml'))
    def test_get_invite_user_list(self, userinfo, get_env_info):
        #获取可以被邀请至某个集群的用户列表接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url']+'?cluster-name=host'
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/9_invite_user_to_cluster.yaml'))
    def test_invite_user_to_cluster(self, userinfo, get_env_info):
        #邀请用户到集群接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url'].replace('{user-name}',globals()['username']).replace('{cluster-name}','host').replace('{cluster-role}','cluster-admin')
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/10_delete_user_from_cluster.yaml'))
    def test_delete_user_from_cluster(self, userinfo, get_env_info):
        #把用户从集群中移除接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url'].replace('{user-name}',globals()['username']).replace('{cluster-name}','host')
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('userinfo', YamlUtil().read_testcase_yaml('v24.09/userManagement/11_delete_user.yaml'))
    def test_delete_user(self, userinfo, get_env_info):
        # 删除用户接口
        baseUrl = get_env_info
        url = baseUrl + userinfo['request']['url'].replace('{username}',globals()['username'])
        method = userinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == userinfo['validate'][0]['equals']['status_code']